﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Reflection;
using AgencyManagerSystem.Model;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.DataFetcher;


namespace AgencyManagerSystem.Core
{

    public class PuOrderUpdaterCreator : MainDetailUpdaterCreator<PuOrderModel, PuOrderDetailModel>
    {
        PuOrderFetcher puOrderFetcher = new PuOrderFetcher();
        PuOrderDetailFetcher puOrderDetailFetcher = new PuOrderDetailFetcher();
        public PuOrderUpdater CreatePuOrderUpdaterInstance()
        {
            var mainUpdater = PuOrderUpdater.GetMainUpdater();
            mainUpdater.DataUpdating += FlagAndApproveFlagValid;
            mainUpdater.DataUpdating += ReferExist;
            mainUpdater.DataUpdating += StatusValid;
            mainUpdater.DataDeleting += DeleteValid;
            PuOrderUpdater puUpdater = new PuOrderUpdater(puOrderFetcher, puOrderDetailFetcher, mainUpdater);
            puUpdater.DataBulkUpdating += ProductionValid;
            puUpdater.DataBulkUpdating += CommonValid;
            return puUpdater;
        }

        private void DeleteValid(BaseUpdater<PuOrderModel> sender, DataDeletingEventArgs<PuOrderModel> e)
        {
            if (e.Delete == null) return;
            if(e.Delete.Status!= (int)StatusCode.Open)
            {
                e.Message = String.Format("单据{0}不是开立状态不能删除",e.Delete.Code);
                e.Cancel = true;
                return;
            }
        }
        private string ValidDimension(PuOrderDetailModel item, ProductionModel production)
        {
            var dimensionDocFetcher = new DimensionDocFetcher();
            for (int i = 1; i <= 10; i++)
            {
                PropertyInfo prodDimension = typeof(ProductionModel).GetProperty("Dimension" + i);
                if ((bool)prodDimension.GetValue(production))
                {
                    PropertyInfo itemDimension = typeof(PuOrderDetailModel).GetProperty("Dimension" + i);
                    int dimensionInItem = (int)itemDimension.GetValue(item);
                    var docs = dimensionDocFetcher.ListDimensionByProduction(production.AutoId, i);
                    var records = from dimension in docs where dimension.AutoId == dimensionInItem select dimension;
                    if (records.Count() == 0)
                    {
                        return String.Format("物料{0}的纬度{2}选项{1}不存在", item.ProductionId, dimensionInItem, i);
                    }
                }
                else if (item.Dimension1 != 0)
                {
                    return String.Format("物料{0}的纬度{1}不启用，不允许录入资料", item.ProductionId, i);
                }
            }
            return "";
        }
        private void ProductionValid(MainDetailUpdater<PuOrderModel, PuOrderDetailModel> sender, DataBulkUpdatingEventArgs<PuOrderModel, PuOrderDetailModel> e)
        {
            if (e.DetailInsert == null)
                return;
            var productionFetcher = new ProductionFetcher();
            foreach(var item in e.DetailInsert)
            {
                var production = productionFetcher.Get(item.ProductionId);
                if(production==null)
                {
                    e.Cancel = true;
                    e.Message = String.Format("找不到物料ID={0}的资料", item.ProductionId);
                    return;
                }
                string message = ValidDimension(item, production);
                if(String.IsNullOrWhiteSpace(message))
                {
                    e.Cancel = true;
                    e.Message = message;
                    return;
                }
                if(String.IsNullOrWhiteSpace(item.ProductionCode)|| String.IsNullOrWhiteSpace(item.ProductionName)|| String.IsNullOrWhiteSpace(item.Unit) )
                {
                    e.Cancel = true;
                    e.Message = String.Format("物料{0}的品名，品号，单位不能为空", item.ProductionId);
                    return;
                }
                if(item.Qty<=0)
                {
                    e.Cancel = true;
                    e.Message = String.Format("物料{0}的数量必须大于0", item.ProductionId);
                    return;
                }
            }
        }

        private void StatusValid(BaseUpdater<PuOrderModel> sender, DataUpdatingEventArgs<PuOrderModel> e)
        {
            if (e.Insert == null)
                return;
            if (e.Insert.ApproveFlag != 0)
            {
                e.Cancel = true;
                e.Message = String.Format("单据{0}不是开立状态不能修改", e.Insert.Code);
                return;
            }
            if (e.Insert.Status == (int)StatusCode.Closed && String.IsNullOrWhiteSpace(e.Insert.CloseReason))
            {
                e.Cancel = true;
                e.Message = String.Format("单据{0}是关闭状态下必须指定关闭原因", e.Insert.Code);
                return;
            }
        }

        private void ReferExist(BaseUpdater<PuOrderModel> sender, DataUpdatingEventArgs<PuOrderModel> e)
        {
            if (e.Insert == null)
                return;
            var supplierFetcher = new SupplierFetcher();
            var suppiler = supplierFetcher.Get(e.Insert.Suppiler);
            if(suppiler==null)
            {
                e.Cancel = true;
                e.Message = String.Format("无法找到供应商{0}对应的档案",e.Insert.Suppiler);
                return;
            }
            var purchFetcher = new UserFetcher();
            var purch = purchFetcher.Get(e.Insert.Purchaser);
            if(purch==null)
            {
                e.Cancel = true;
                e.Message = String.Format("无法找到采购员{0}对应的档案", e.Insert.Purchaser);
                return;
            }
            if(String.IsNullOrWhiteSpace(e.Insert.Code))
            {
                e.Cancel = true;
                e.Message = "订单编码不能为空";
                return;
            }
            DateTime dt;
            if(!DateTime.TryParse(e.Insert.DocDate,out dt))
            {
                e.Cancel = true;
                e.Message = String.Format("{0}不是正确的日期格式", e.Insert.DocDate);
                return;
            }
        }
    }
}